<?php
namespace App\Model;
use Core\Component\Di;
use App\Vendor\StringHelper;

class ChatNotice{
    protected $db;
    protected $tableName = 'chat_notice';

    function __construct(){
        $db = Di::getInstance()->get("MYSQL");
        if($db instanceof \MysqliDb){
            $this->db = $db;
        }
    }

    public function addNotice($sendId,$methodId,$methodType,$applyMsg){
        if(!$sendId || !$methodId){
            return FALSE;
        }
        $data['send_id']=$sendId;
        $data['method_id']=$methodId;
        $data['type']=$methodType;
        $data['apply_msg']=$applyMsg;
        $data['apply_time']=StringHelper::getDateTime(TRUE);
        $data['is_read']=0;
        $data['status']=0;
        $id = $this->db->insert($this->tableName,$data);
        if(!$id){
            return FALSE;
        }
        return TRUE;
    }

    public function editNotice($sendId,$methodId,$methodType,$method){
        if(!$sendId || !$methodId || !$methodType || !$method){
            return FALSE;
        }
        $this->db->where("send_id", $sendId);
        $this->db->where("method_id", $methodId);
        $this->db->where("type", $methodType);
        if($method==0){
            $data['is_read'] = 0;
            $data['status'] = -1;
            $data['refuse_time'] = StringHelper::getDateTime(TRUE);
        }elseif($method==1){
            $data['is_read'] = 0;
            $data['status'] = 1;
            $data['pass_time'] = StringHelper::getDateTime(TRUE);
        }else{
            return FALSE;
        }
        $id = $this->db->update($this->tableName,$data);
        if(!$id){
            return FALSE;
        }
        if($data['status'] == 1){
            if($methodType == 1){
                (new FriendRelation())->addFriend($methodId, $sendId);
            }elseif($methodType == 2){
                (new GroupRelation())->userIntoGroup($sendId,$methodId);
            }else{
                (new GroupRelation())->userIntoGroup($methodId,$sendId);
            }
        }
        return TRUE;
    }

    public function getNoticeNum($userId,$setRead = false){
        $num = 0;
        if(!$userId){
            return $num;
        }
        $groupIds = (new ChatGroup())->getOwnerGroup($userId);
        $num += $this->getFriendApply($userId,$setRead);
        $num += $this->getFriendNotice($userId,$setRead);
        if(!empty($groupIds)){
            $num += $this->getMemberApply($groupIds,$setRead);
            $num += $this->getMemberNotice($groupIds,$setRead);
        }
        return $num;
    }

    private function getFriendApply($methodId,$setRead = false){
        $this->db->where("method_id", $methodId);
        $this->db->where("status", 0);
        $this->db->where("is_read", 0);
        if (!$setRead){
            $column = 'count(id) as num';
            $info = $this->db->getOne($this->tableName,$column);
            if(!$info){
                return 0;
            }
            return $info['num'];
        }else{
            $data["is_read"]=1;
            $this->db->update($this->tableName,$data);
        }
    }

    private function getFriendNotice($methodId,$setRead = false){
        $this->db->where("send_id", $methodId);
        $this->db->where("status", [1,-1], 'in');
        $this->db->where("is_read", 0);
        if (!$setRead){
            $column = 'count(id) as num';
            $info = $this->db->getOne($this->tableName,$column);
            if(!$info){
                return 0;
            }
            return $info['num'];
        }else{
            $data["is_read"]=1;
            $this->db->update($this->tableName,$data);
        }
    }

    private function getMemberApply($methodId,$setRead = false){
        $this->db->where("method_id", $methodId,"IN");
        $this->db->where("status", 0);
        $this->db->where("is_read", 0);
        $this->db->where("type", 2,">=");
        if (!$setRead){
            $column = 'count(id) as num';
            $info = $this->db->getOne($this->tableName,$column);
            if(!$info){
                return 0;
            }
            return $info['num'];
        }else{
            $data["is_read"]=1;
            $this->db->update($this->tableName,$data);
        }
    }

    private function getMemberNotice($methodId,$setRead = false){
        $this->db->where("send_id", $methodId,"IN");
        $this->db->where("status", [1,-1], 'in');
        $this->db->where("is_read", 0);
        $this->db->where("type", 2,">=");
        if (!$setRead){
            $column = 'count(id) as num';
            $info = $this->db->getOne($this->tableName,$column);
            if(!$info){
                return 0;
            }
            return $info['num'];
        }else{
            $data["is_read"]=1;
            $this->db->update($this->tableName,$data);
        }
    }

    public function getAllNotice($userId,$pageNum=1,$pageSize=10){
        if(!$userId){
            return [];
        }
        if($pageNum == 1){
            $this->getNoticeNum($userId,1);
        }
        $ownerGroupList = (new ChatGroup())->getOwnerGroup($userId);
        if(!$ownerGroupList){
            $idList[]=$userId;
        }else{
            $idList = $ownerGroupList;
            $idList[]=$userId;
        }
        $this->db->where("send_id", $idList,'IN','or');
        $this->db->where("method_id", $idList,'IN','or');
        $this->db->orderBy("id","desc");
        $column = 'id as noticeId,send_id as sendId,method_id as methodId,type as noticeType,apply_msg as applyMsg,'
                . 'apply_time as applyTime,pass_time as passTime,refuse_time as refuseTime,is_read as isRead,status';
        $this->db->pageLimit = $pageSize;
        $products = $this->db->arraybuilder()->paginate($this->tableName, $pageNum,$column);
        if(!$products){
            return [];
        }
        $list['totalPage']=$this->db->totalPages;
        $list['totalCount']=$this->db->totalCount;
        $list['users']=$products;
        return $list;
    }
}
